<template>
  <header class="header">
    <h1>todos</h1>
    <input :disabled="list.length === 0" v-model="isAll" id="toggle-all" class="toggle-all" type="checkbox" >
    <label for="toggle-all"></label>
    <input
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      autofocus
      v-model.trim="taskname"
      @keydown.enter="add(taskname),taskname=''"
    />
  </header>
</template>

<script>

import { mapState , mapMutations } from 'vuex'

export default {
  data() {
    return {
      taskname: ''
    }
  },
  methods: {
    ...mapMutations('TodoMain',['add']),
    ...mapMutations('TodoMain',['setAll'])
   
  },
  computed: {
    ...mapState('TodoMain',['list']),
    isAll: {
      set(val) {
        this.setAll(val)
      },
      get() {
        // 根据小选框的选中状态来决定
        // 如果全部选中就返回 true
        // 只要有一个没选中就返回 false
        if (this.list.length === 0) return false
        return this.list.every(item => item.isDone)
      }
    }
  }
}
</script>